﻿// Bartlomiej Galkowski 2012
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PyroGen;

namespace PyroGenTest
{
    [TestClass]
    public class SlicerTest
    {
        [TestMethod]
        public void SnippetTest()
        {
            string text = "fooo";
            CodeType type = CodeType.Expression;

            Snippet snip = new Snippet(text, type);
            Assert.AreEqual(snip.Text, text);
            Assert.AreEqual(snip.Type, type);
        }

        [TestMethod]
        public void SlicerEmptyText()
        {
            string str = "";

            Dictionary<string, Tuple<CodeType, Terminator>> dict = new Dictionary<string, Tuple<CodeType, Terminator>>();
            dict.Add("aa", new Tuple<CodeType, Terminator>(CodeType.ScriptCode, Terminator.Start));
            dict.Add("bb", new Tuple<CodeType, Terminator>(CodeType.ScriptCode, Terminator.End));
            Slicer slicer = new Slicer(str, dict);

            List<Snippet> snipp = slicer.GetSlicedSource();

            Assert.AreEqual<int>(1, snipp.Count);
            Assert.AreEqual(new Snippet("",CodeType.Text), snipp[0]);

        }

        [TestMethod]
        public void SlicerEmptyDict()
        {
            string str = "Edyta";
            Dictionary<string, Tuple<CodeType, Terminator>> dict = new Dictionary<string, Tuple<CodeType, Terminator>>();
            Slicer slicer = new Slicer(str, dict);
            List<Snippet> list = slicer.GetSlicedSource();
            Assert.AreEqual<int>(list.Count, 1);
            Assert.AreEqual<Snippet>(new Snippet(str,CodeType.Text), list[0]);

        }


        [TestMethod]
        public void SlicerSimpleTest()
        {
            string pre = "cabaaba";
            string keyw1 = "cb";
            string word = "abaca";
            string keyw2 = "bc";
            string post = "faafaca";

            Dictionary<string, Tuple<CodeType, Terminator>> dict = new Dictionary<string, Tuple<CodeType, Terminator>>();
            dict.Add(keyw1, new Tuple<CodeType, Terminator>(CodeType.ScriptCode, Terminator.Start));
            dict.Add(keyw2, new Tuple<CodeType, Terminator>(CodeType.ScriptCode, Terminator.End));
            Slicer slicer = new Slicer(pre+keyw1+word+keyw2+post, dict);
           
            List<Snippet> list = slicer.GetSlicedSource();
            Assert.AreEqual(list.Count, 3);
            Assert.AreEqual(new Snippet(pre,CodeType.Text), list[0]);
            Assert.AreEqual(new Snippet(word,CodeType.ScriptCode), list[1]);
            Assert.AreEqual(new Snippet(post,CodeType.Text), list[2]);

        }

        [TestMethod]
        public void SlicerOverlapTest()
        {

            string textPref = "maba";
            string textWord = "foo";
            string textPost = "mam"; 
            
            string key3 = "<.";
            string key4 = ".>";
            string key1 = "<.=";
            string key2 = "=.>";
            string text = textPref + key1 + textWord + key2 + textPost;


            Dictionary<string, Tuple<CodeType, Terminator>> dict = new Dictionary<string, Tuple<CodeType, Terminator>>();
            dict.Add(key1, new Tuple<CodeType, Terminator>(CodeType.Expression, Terminator.Start));
            dict.Add(key2, new Tuple<CodeType, Terminator>(CodeType.Expression, Terminator.End));
            dict.Add(key3, new Tuple<CodeType, Terminator>(CodeType.ScriptCode, Terminator.Start));
            dict.Add(key4, new Tuple<CodeType, Terminator>(CodeType.ScriptCode, Terminator.End));

            Slicer slicer = new Slicer(text,dict);
            List<Snippet> list = slicer.GetSlicedSource();
            Assert.AreEqual(3, list.Count);

            Assert.AreEqual(list[0], new Snippet(textPref, CodeType.Text));
            Assert.AreEqual(list[1], new Snippet(textWord, CodeType.Expression));
            Assert.AreEqual(list[2], new Snippet(textPost, CodeType.Text));

            //Assert.AreEqual(new Snippet(key1, CodeType.ConfigCode), );
            


        }
    }
}
